//
//  UIImageView+ZUtilities.m
//  WeClubs
//
//  Created by Zed Link on 28/3/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "UIImageView+ZUtilities.h"

@implementation UIImageView (ZUtilities)

+ (instancetype)zImageViewWithImage:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    NSString *desc = [NSString stringWithFormat:@"找不到与文件名\"%@\"所对应的图片", imageName];
    NSAssert(image != nil, desc);

    return [[UIImageView alloc] initWithImage:image];
}

- (UIImage *)imageWithColor:(UIColor *)color
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, self.image.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    CGContextClipToMask(context, rect, self.image.CGImage);
    
    [color setFill];
    CGColorSpaceRef colorSpaceRef=CGColorSpaceCreateDeviceRGB();
    
    CGFloat colorsAll[4]={0.0,0.0,0.0,0.0};
    NSArray *arr = [UIImageView colorForGGB:color];
    colorsAll[0] = [arr[0] floatValue];
    colorsAll[1] = [arr[1] floatValue];
    colorsAll[2] = [arr[2] floatValue];
    colorsAll[3] = 1.0;
    CGFloat locations[2]={1,1.0};
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpaceRef, colorsAll, locations,2);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(self.frame.size.width, kCGGradientDrawsAfterEndLocation), kCGGradientDrawsBeforeStartLocation);
    
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    self.image = newImage;
    
    return newImage;
}

- (UIImage *)imageWithColors:(UIColor *)colors
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, self.image.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    CGContextClipToMask(context, rect, self.image.CGImage);
    
//    [color setFill];
    CGColorSpaceRef colorSpaceRef=CGColorSpaceCreateDeviceRGB();
    
    CGFloat colorsAll[4]={0.0,0.0,0.0,0.0};
    NSArray *arr = [UIImageView colorForGGB:colors];
    colorsAll[0] = [arr[0] floatValue];
    colorsAll[1] = [arr[1] floatValue];
    colorsAll[2] = [arr[2] floatValue];
    colorsAll[3] = 1.0;
    CGFloat locations[2]={1,1.0};
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpaceRef, colorsAll, locations,2);
    
    
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(self.frame.size.width, kCGGradientDrawsAfterEndLocation), kCGGradientDrawsBeforeStartLocation);
    
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    self.image = newImage;
    
    return newImage;
}

// 返回颜色RBG值
+ (NSArray *)colorForGGB:(UIColor *)color {
    
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    return @[@(components[0]),@(components[1]),@(components[2])];
}

@end
